﻿//NorManager - OpenTTD Dedicated server manager
//Copyright (C) 2014 Tomáš Bosek

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NorManager
{
    class Program
    {
        static void Main(string[] args)
        {
            StartArguments arguments = new StartArguments();
            CommandLine.Parser parser = new CommandLine.Parser();
            bool parsed = parser.ParseArguments(args, arguments);
            if (args.Length == 0 || !parsed)
            {
                arguments.GetUsage();
                return;
            }

            Logger.Instance.Log("Arguments OK");

            Session.Instance.StartSession(new SessionConfig(arguments.Executable, arguments.Config, arguments.Autoload));
            while (true)
            {
                string command = Console.ReadLine();
                if (command == "start")
                {
                    Session.Instance.StartSession();
                    continue;
                }
                else if (command == "stop")
                {
                    Session.Instance.CloseSession();
                    continue;
                }
                else if (command == "reload" || command == "restart")
                {
                    Session.Instance.ReloadSession();
                    continue;
                }
                if (Session.Instance.State == SessionState.Running)
                    Session.Instance.Writer.Write(command);
            }
        }
    }
}
